package com.dxl.web.common.handler;

import java.lang.reflect.UndeclaredThrowableException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.dxl.common.model.RespData;

/**
 * Spring MVC全局异常处理类
 * @author 邓小林
 */
public class ExceptionHandler implements HandlerExceptionResolver {
	private static final Logger LOG = Logger.getLogger(ExceptionHandler.class);
	
	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
		String msg = ex.getMessage();
		if (ex instanceof UndeclaredThrowableException) {
			Throwable cause = ex.getCause();
			msg = cause.getMessage();
		}
		Integer code = null;
		
		
		LOG.error(msg, ex);
		ModelAndView mv = new ModelAndView("error");
		mv.addObject("data", RespData.instance(false, msg, code));
		return mv;
	}
}
